package org.zuel.huhuForum.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;
import org.zuel.huhuForum.constant.CommunityConstant;
import org.zuel.huhuForum.model.DiscussPost;
import org.zuel.huhuForum.model.User;
import org.zuel.huhuForum.param.Page;
import org.zuel.huhuForum.service.IDiscussPostService;
import org.zuel.huhuForum.service.IElasticsearchService;
import org.zuel.huhuForum.service.ILikeService;
import org.zuel.huhuForum.service.IUserService;
import org.zuel.huhuForum.service.Impl.HistoryService;
import org.zuel.huhuForum.util.HostHolder;
import java.util.*;
import static org.zuel.huhuForum.util.DesensitizeUtil.desensitize;

@Log4j2
@RestController
@RequestMapping(value = "/index")
@Api(tags = "进入主页")
public class IndexController implements CommunityConstant{
    @Autowired
    private IDiscussPostService discussPostService;

    @Autowired
    private IUserService userService;

    @Autowired
    private ILikeService likeService;

    @Autowired
    private IElasticsearchService elasticsearchService;

    @Autowired
    private HistoryService historyService;

    @Autowired
    private HostHolder hostHolder;

    /**
     * 进入热榜
     * @param current
     * @param orderMode 默认是 0（最新）
     * @return
     */
    @ApiOperation(value = "进入热榜")
    @PostMapping("/indexhot")
    public Map<String, Object> getIndexPage(@RequestParam(name = "current",required = false,defaultValue ="1") Integer current,
                                            @RequestParam(name = "orderMode", defaultValue = "1") int orderMode) {
        Map<String, Object>model=new HashMap<>();
        Page page=new Page();
        if(current!=null){
            page.setCurrent(current);
        }
        // 获取总页数
        page.setRows(discussPostService.findDiscussPostRows(0));
        page.setPath("/index?orderMode=" + orderMode);

        // 分页查询
        List<DiscussPost> list = discussPostService.findDiscussPosts(0, page.getOffset(), page.getLimit(), orderMode);
        // 封装帖子和该帖子对应的用户信息
        List<Map<String, Object>> discussPosts = new ArrayList<>();
        if (list != null) {
            for (DiscussPost post : list) {
                Map<String, Object> map = new HashMap<>();
                map.put("post", post);
                User user = userService.findUserById(post.getUserId());
                map.put("user", desensitize(user));
                long likeCount = likeService.findEntityLikeCount(CommunityConstant.ENTITY_TYPE_POST, post.getId());
                map.put("likeCount", likeCount);

                discussPosts.add(map);
            }
        }
        model.put("discussPosts", discussPosts);
        model.put("orderMode", orderMode);
        return model;
    }
    /**
     * 首页推荐
     * @param current
     * @return
     */
    @ApiOperation(value = "首页推荐")
    @PostMapping("/index")
    public Map<String, Object> getIndex(@RequestParam(name = "current",required = false,defaultValue ="1") Integer current) {
        Map<String, Object>model=new HashMap<>();
        Page page=new Page();
        if(current!=null){
            page.setCurrent(current);
        }
        Set<DiscussPost> set=new HashSet<>();
        User user;
        if(hostHolder.getUser()!=null){
            user= hostHolder.getUser();
            List<String> strings=historyService.getHistoryAndRead(user.getId());
            Random rand = new Random();
            int j=0;

            while(j<5||j<strings.size()){
                if(strings.size()==0){
                    break;
                }
                List<DiscussPost> discussPosts=elasticsearchService.searchs(strings.get(rand.nextInt(strings.size())),0,10);
                for(DiscussPost post:discussPosts){
                    Integer flage=new Random().nextInt(10);
                    if(post.getStatus().equals(TYPE_TWE)||flage<=6){
                        continue;
                    }
                    set.add(post);
                }
                j++;
            }
        }else{
            for(DiscussPost post:discussPostService.findDiscussPosts(0, 1, 80, 1)){
                if(post.getStatus().equals(TYPE_TWE)){
                    continue;
                }
                Integer flage=new Random().nextInt(100);
                if(flage>35){
                    set.add(post);
                }
            }
        }
        for(DiscussPost post:discussPostService.findDiscussPosts(0, 1, 40, 1)){
            Integer flage=new Random().nextInt(10);
            if(post.getStatus().equals(TYPE_TWE)||flage<5){
                continue;
            }

            set.add(post);
        }
        page.setRows(set.size());
        page.setLimit(10);
        // 封装帖子和该帖子对应的用户信息
        List<Map<String, Object>> discussPosts = new ArrayList<>();
        if (set != null) {
            Integer i=0;
            for(DiscussPost post :set){
                Integer flage=new Random().nextInt(100);
                if(post.getStatus().equals(TYPE_TWE)||flage<45){
                    continue;
                }
                i++;
                if(i<page.getOffset()){
                    continue;
                }
                if(i>page.getOffset()+15){
                    break;
                }
                Map<String, Object> map = new HashMap<>();
                map.put("post", post);
                user = userService.findUserById(post.getUserId());
                map.put("user", desensitize( user));
                long likeCount = likeService.findEntityLikeCount(CommunityConstant.ENTITY_TYPE_POST, post.getId());
                map.put("likeCount", likeCount);
                int likeStatus = hostHolder.getUser() == null ? 0 :
                        likeService.findEntityLikeStatus(hostHolder.getUser().getId(), CommunityConstant.ENTITY_TYPE_POST, post.getId());
                model.put("likeStatus", likeStatus);
                discussPosts.add(map);
            }
        }
        model.put("discussPosts", discussPosts);
        model.put("page",page);
        return model;
    }

}
